import java.sql.*;
import java.util.Scanner;

public class Sy2_3 {
    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/school?useSSL=false";
    private static String user="root";
    private static String password="123456";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn= DriverManager.getConnection(url,user,password);
        Scanner sc=new Scanner(System.in);

        String sqlSelect="select * from ben where name=?";
        System.out.println("请输入要查询记录的姓名：");
        String a= sc.next();
        PreparedStatement pstmt1=conn.prepareStatement(sqlSelect);
        pstmt1.setString(1,a);
        ResultSet rs=pstmt1.executeQuery();
        while(rs.next()){
            String tel=rs.getString(1);
            String name=rs.getString(2);
            String sex=rs.getString(3);
            String adress=rs.getString(4);
            System.out.println("手机号码："+tel+" 姓名："+name+" 性别："+sex+" 地址："+adress);
        }

        String sqlInsert="insert into user(id,password) values(?,?)";
        System.out.println("请输入要插入记录的id：");
        String b1=sc.next();
        System.out.println("请输入要插入记录的密码：");
        String b2=sc.next();
        PreparedStatement pstmt2=conn.prepareStatement(sqlInsert);
        pstmt2.setString(1,b1);
        pstmt2.setString(2,b2);
        int bb=pstmt2.executeUpdate();
        System.out.println("插入操作影响"+bb+"条数据");

        String sqlUpdate="update ben set tel=? where name=?";
        System.out.println("请输入要更新记录的姓名：");
        String c1=sc.next();
        System.out.println("要将手机号码更改为：");
        String c2=sc.next();
        PreparedStatement pstmt3=conn.prepareStatement(sqlUpdate);
        pstmt3.setString(1,c1);
        pstmt3.setString(2,c2);
        int cc=pstmt3.executeUpdate();
        System.out.println("更新操作影响"+cc+"条记录");

        String sqlDelete="delete from ben where name=?";
        System.out.println("请输入要删除记录的姓名：");
        String d=sc.next();
        PreparedStatement pstmt4=conn.prepareStatement(sqlDelete);
        pstmt4.setString(1,d);
        int dd=pstmt4.executeUpdate();
        System.out.println("删除操作影响"+dd+"条记录");
    }
}
